package com.wing.wecom.controller;/*
package com.wing.wecom.controller;

import chat.qiye.wechat.sdk.api.oa.OaApprovalApi;
import chat.qiye.wechat.sdk.api.oa.param.GetApprovalDetailParam;
import chat.qiye.wechat.sdk.api.oa.resp.ApprovalDetailResp;
import chat.qiye.wechat.sdk.api.oa.vo.approval.TitleItem;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wing.building.model.entity.*;
import com.wing.building.model.request.CheckOutApplyForm;
import com.wing.building.model.request.DeployApplyForm;
import com.wing.building.model.request.LiveApplyForm;
import com.wing.building.service.FloorService;
import com.wing.building.service.RoomCategoryService;
import com.wing.building.service.RoomSnService;
import com.wing.common.Global;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.system.model.entity.User;
import com.wing.user.model.entity.*;
import com.wing.user.model.request.*;
import com.wing.user.service.LeaveTypeService;
import com.wing.system.service.UserService;
import com.wing.wecom.model.request.ApproveForm;
import com.wing.wecom.service.ReceiveService;
import com.wing.wecom.util.wx.AesException;
import com.wing.wecom.util.wx.WXBizMsgCrypt;
import com.wing.wecom.util.wx.WxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import weixin.popular.support.ExpireKey;
import weixin.popular.support.expirekey.DefaultExpireKey;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

*/
/**
 * 接受消息推送
 *
 * @author
 *//*

@Slf4j
@RestController
public class ReceiveController {

    private final String token = "Yicremx2keJkJsSPhmogmjdt6";
    private final String encodingAesKey = "obLJ5NdGqSCvNvmKe5TwJlLRqg2cdj4W5M4eVOoMueN";
    private final String receiveid = "wwbefee404a6a5cadf";

    */
/**
     * 重复通知过滤
     *//*

    private static ExpireKey expireKey = new DefaultExpireKey();

    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private LeaveTypeService leaveTypeService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoomCategoryService roomCategoryService;
    @Autowired
    private RoomSnService roomSnService;
    @Autowired
    private FloorService floorService;
    @Resource
    OaApprovalApi oaApprovalApi;

    */
/**
     * 微信回调
     *
     * @return
     *//*

    @GetMapping("/callback")
    public String receive(HttpServletRequest request, HttpServletResponse response) throws AesException {
        String msg_signature = request.getParameter("msg_signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        //验证请求签名
        WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, receiveid);
        String result = wxBizMsgCrypt.VerifyURL(msg_signature, timestamp, nonce, echostr);
        return result;
    }

    */
/**
     * 微信回调
     *
     * @return
     *//*

    @PostMapping("/callback")
    public String receive(HttpServletRequest request) throws Exception {
        String msg_signature = request.getParameter("msg_signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");

        //验证请求签名
        WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(token, encodingAesKey, receiveid);
        String postData = ""; // 密文，对应POST请求的数据
        // 1.获取加密的请求消息；使用输入流获得加密请求消息postData
        ServletInputStream inputStream = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String tempStr = ""; // 作为输出字符串的临时串，用于判断是否读取完毕
        while (null != (tempStr = reader.readLine())) {
            postData += tempStr;
        }

        String xml = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, postData);
        System.out.println(xml);
        ApproveForm approveForm = WxUtil.parseXml(xml);
        System.out.println(approveForm);
        // 对获取的信息进行处理
        dataProcess(approveForm);
        return xml;
    }

    private void dataProcess(ApproveForm approveForm) {
        String name = approveForm.getSpName(); // 审批申请类型名称（审批模板名称）
        // 审批申请状态变化类型：1-提单；2-同意；3-驳回；4-转审；5-催办；6-撤销；8-通过后撤销；10-添加备注
        Integer statuChangeEvent = approveForm.getStatuChangeEvent();
        String spNo = approveForm.getSpNo(); // 审批编号
        // 通过spNo获取审批申请详情
        GetApprovalDetailParam getApprovalDetailParam = new GetApprovalDetailParam();
        getApprovalDetailParam.setSpNo(spNo);
        ApprovalDetailResp approvalDetailResp = oaApprovalApi.getDetail(getApprovalDetailParam);
        */
/**
         * 说明：
         * 1.不同的模板名称匹配不同的申请
         * 2.通过审批申请状态变化类型来确认当前回调过来的是那种状态的申请，然后判断是创建申请，还是修改申请等。
         *//*

        if(name.equals(Global.LEAVE)){ // 请假
            if(statuChangeEvent == 1){ // 创建请假申请
                LeaveApplyForm leaveApplyForm = new LeaveApplyForm();
                leaveApplyForm.setLeaveNumber(approveForm.getSpNo());
                leaveApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId()));
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Vacation")){ // 假勤组件-请假组件
                        // 假勤组件类型：1-请假；2-补卡；3-出差；4-外出；5-加班
                        Integer type = contentsItem.getValue().getVacation().getAttendance().getType();
                        if(type == 1){ // 请假
                            String leaveTypeName = contentsItem.getValue().getVacation().getSelector().getOptions().get(0).getValue().get(0).getText(); // 请假类型名称
                            // 通过请假类型名称查询请假类型
                            LeaveType leaveType = leaveTypeService.getOne(new QueryWrapper<LeaveType>().eq("name", leaveTypeName));
                            leaveApplyForm.setLeaveTypeId(leaveType.getId());
                            leaveApplyForm.setTimeScale(leaveType.getTimeScale()); // 时间刻度
                            leaveApplyForm.setUnitConvert(leaveType.getUnitConvert()); // 单位换算：每日工作时长
                            // 时间展示类型：halfday-日期；hour-日期+时间
                            String timeScale = contentsItem.getValue().getVacation().getAttendance().getDate_range().getType();
                            Long begin = Long.parseLong(contentsItem.getValue().getVacation().getAttendance().getDate_range().getNew_begin().toString());
                            Long end = Long.parseLong(contentsItem.getValue().getVacation().getAttendance().getDate_range().getNew_end().toString());
                            Integer duration = contentsItem.getValue().getVacation().getAttendance().getDate_range().getNew_duration();
                            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            if(timeScale.equals("hour")){
                                leaveApplyForm.setStartTime(sf.format(new Date(begin*1000)));
                                leaveApplyForm.setEndTime(sf.format(new Date(end*1000)));
                                leaveApplyForm.setDuration(new BigDecimal(duration.toString()).divide(new BigDecimal("3600"), 1, BigDecimal.ROUND_HALF_UP));
                            }else{
                                // 通过时间计算上下午  0点为上午，12点为下午
                                String beginTime = sf.format(new Date(begin*1000));
                                String prefixBegin = beginTime.split(" ")[0];
                                String suffixBegin = beginTime.split(" ")[1];
                                String endTime = sf.format(new Date(end*1000));
                                String prefixEnd = endTime.split(" ")[0];
                                String suffixEnd = endTime.split(" ")[1];
                                if(suffixBegin.equals("00:00:00")){
                                    leaveApplyForm.setStartChoose("上午");
                                }else if(suffixBegin.equals("12:00:00")){
                                    leaveApplyForm.setStartChoose("下午");
                                }
                                if(suffixEnd.equals("12:00:00")){
                                    leaveApplyForm.setEndChoose("上午");
                                }else if(suffixEnd.equals("12:00:00")){
                                    leaveApplyForm.setEndChoose("下午");
                                }
                                leaveApplyForm.setStartTime(prefixBegin);
                                leaveApplyForm.setEndTime(prefixEnd);
                                leaveApplyForm.setDuration(new BigDecimal(duration.toString()).divide(new BigDecimal("86400"), 1, BigDecimal.ROUND_HALF_UP));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Textarea")){ // 文本/多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("请假事由")){
                                leaveApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("File")){ // 附件控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("说明附件")){
                                if(contentsItem.getValue().getFiles().size() > 0){
                                    */
/*String fileId = contentsItem.getValue().getFiles().get(0).getFileId();
                                    // 获取临时素材*//*

                                    leaveApplyForm.setAnnex(null);
                                }
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                leaveApplyForm.setApproverIds(approverIds); // 审批人IDS
                leaveApplyForm.setCcIds(ccIds); // 抄送人IDS
                leaveApplyForm.setStatus(LeaveApply.Status.stayProcess); // 状态
                leaveApplyForm.setSource(LeaveApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addLeaveApply(leaveApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeLeave(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectLeave(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralLeave(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeLeave(spNo);
            }
        }else if(name.equals(Global.SHIFT)){ // 调班
            if(statuChangeEvent == 1){ // 创建请假申请
                ShiftApplyForm shiftApplyForm = new ShiftApplyForm();
                shiftApplyForm.setShiftNumber(approveForm.getSpNo()); // 调班编号
                shiftApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 调班申请人员ID
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("SwitchSchedule")){ // 调班
                        shiftApplyForm.setShiftUserId(Long.parseLong(contentsItem.getValue().getSwitchSchedule().getToUser().getId())); // 调班对象人员ID
                        Long shiftDate = Long.parseLong(contentsItem.getValue().getSwitchSchedule().getSwitchSchedule().getDate().toString());
                        Long alsoShiftDate = Long.parseLong(contentsItem.getValue().getSwitchSchedule().getReturnSchedule().getDate().toString());
                        shiftApplyForm.setShiftDate(LocalDateTimeUtils.convertDateToLDT(new Date(shiftDate*1000))); // 调班日期
                        shiftApplyForm.setAlsoShiftDate(LocalDateTimeUtils.convertDateToLDT(new Date(alsoShiftDate*1000))); // 还班日期
                    }else if(contentsItem.getControl().equals("Textarea")){ // 文本/多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("调班事由")){
                                shiftApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                shiftApplyForm.setApproverIds(approverIds); // 审批人IDS
                shiftApplyForm.setCcIds(ccIds); // 抄送人IDS
                shiftApplyForm.setStatus(ShiftApply.Status.stayProcess); // 状态
                shiftApplyForm.setSource(ShiftApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addShiftApply(shiftApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeShift(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectShift(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralShift(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeShift(spNo);
            }
        }else if(name.equals(Global.GO_OUT)){ // 外出
            if(statuChangeEvent == 1){ // 创建外出申请
                GoOutApplyForm goOutApplyForm = new GoOutApplyForm();
                goOutApplyForm.setGoOutNumber(approveForm.getSpNo()); // 外出编号
                goOutApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 外出申请人员ID
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Attendance")){ // 外出
                        Long startTime = Long.parseLong(contentsItem.getValue().getAttendance().getDateRange().getNewBegin().toString());
                        Long endTime = Long.parseLong(contentsItem.getValue().getAttendance().getDateRange().getNewEnd().toString());
                        Integer duration = contentsItem.getValue().getAttendance().getDateRange().getNewDuration();
                        goOutApplyForm.setStartTime(LocalDateTimeUtils.convertDateToLDT(new Date(startTime*1000))); // 开始时间
                        goOutApplyForm.setEndTime(LocalDateTimeUtils.convertDateToLDT(new Date(endTime*1000))); // 结束时间
                        goOutApplyForm.setDuration(new BigDecimal(duration.toString()).divide(new BigDecimal("3600"), 1, BigDecimal.ROUND_HALF_UP));
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("外出事由")){
                                goOutApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("Text")){ // 文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("外出地点")){
                                goOutApplyForm.setAdress(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("File")){ // 附件控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("附件")){
                                goOutApplyForm.setAnnex(null);
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                goOutApplyForm.setApproverIds(approverIds); // 审批人IDS
                goOutApplyForm.setCcIds(ccIds); // 抄送人IDS
                goOutApplyForm.setStatus(GoOutApply.Status.stayProcess); // 状态
                goOutApplyForm.setSource(GoOutApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addGoOutApply(goOutApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeGoOut(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectGoOut(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralGoOut(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeGoOut(spNo);
            }
        }else if(name.equals(Global.WORK_OVERTIME)){ // 加班
            if(statuChangeEvent == 1){ // 创建加班申请
                WorkOvertimeApplyForm workOvertimeApplyForm = new WorkOvertimeApplyForm();
                workOvertimeApplyForm.setWorkOvertimeNumber(approveForm.getSpNo()); // 加班编号
                workOvertimeApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 加班人员ID
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Attendance")){ // 加班
                        Long startTime = Long.parseLong(contentsItem.getValue().getAttendance().getDateRange().getNewBegin().toString());
                        Long endTime = Long.parseLong(contentsItem.getValue().getAttendance().getDateRange().getNewEnd().toString());
                        Integer duration = contentsItem.getValue().getAttendance().getDateRange().getNewDuration();
                        workOvertimeApplyForm.setStartTime(LocalDateTimeUtils.convertDateToLDT(new Date(startTime*1000))); // 开始时间
                        workOvertimeApplyForm.setEndTime(LocalDateTimeUtils.convertDateToLDT(new Date(endTime*1000))); // 结束时间
                        workOvertimeApplyForm.setDuration(new BigDecimal(duration.toString()).divide(new BigDecimal("3600"), 1, BigDecimal.ROUND_HALF_UP));
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("加班事由")){
                                workOvertimeApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                workOvertimeApplyForm.setApproverIds(approverIds); // 审批人IDS
                workOvertimeApplyForm.setCcIds(ccIds); // 抄送人IDS
                workOvertimeApplyForm.setStatus(WorkOvertimeApply.Status.stayProcess); // 状态
                workOvertimeApplyForm.setSource(WorkOvertimeApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addWorkOvertimeApply(workOvertimeApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeWorkOvertime(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectWorkOvertime(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralWorkOvertime(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeWorkOvertime(spNo);
            }
        }else if(name.equals(Global.PRINT)){ // 用印
            if(statuChangeEvent == 1){ // 创建用印申请
                PrintApplyForm printApplyForm = new PrintApplyForm();
                printApplyForm.setPrintNumber(approveForm.getSpNo()); // 用印编号
                printApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 用印人员ID
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("用印事由")){
                                printApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("Selector")){ // 单选/多选控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("印章类型")){
                                String type = contentsItem.getValue().getSelector().getType();
                                if(type.equals("single")){ // 单选
                                    String text = contentsItem.getValue().getSelector().getOptions().get(0).getValue().get(0).getText(); // 印章类型名称
                                    if(text.equals(PrintApply.SealType.officialSeal.getDesc())){ // 公章
                                        printApplyForm.setSealType(PrintApply.SealType.officialSeal);
                                    }else if(text.equals(PrintApply.SealType.contractSeal.getDesc())){ // 合同章
                                        printApplyForm.setSealType(PrintApply.SealType.contractSeal);
                                    }else if(text.equals(PrintApply.SealType.legalPersonSeal.getDesc())){ // 法人章
                                        printApplyForm.setSealType(PrintApply.SealType.legalPersonSeal);
                                    }else if(text.equals(PrintApply.SealType.other.getDesc())){ // 其他
                                        printApplyForm.setSealType(PrintApply.SealType.other);
                                    }
                                }else if(type.equals("multi")){ // 多选

                                }
                            }
                        }
                    }else if(contentsItem.getControl().equals("Text")){ // 文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("文件名称")){
                                printApplyForm.setName(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("Number")){ // 数字控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("文件份数")){
                                printApplyForm.setNumber(contentsItem.getValue().getNewNumber());
                            }
                        }
                    }else if(contentsItem.getControl().equals("Date")){ // 日期/日期+时间控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("用印日期")){
                                Long usePrintDate = Long.parseLong(contentsItem.getValue().getDate().getTimestamp());
                                printApplyForm.setUsePrintDate(LocalDateTimeUtils.convertDateToLDT(new Date(usePrintDate*1000))); // 用印日期
                            }
                        }
                    }else if(contentsItem.getControl().equals("File")){ // 附件控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("附件")){
                                printApplyForm.setAnnex(null);
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                printApplyForm.setApproverIds(approverIds); // 审批人IDS
                printApplyForm.setCcIds(ccIds); // 抄送人IDS
                printApplyForm.setStatus(PrintApply.Status.stayProcess); // 状态
                printApplyForm.setSource(PrintApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addPrintApply(printApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreePrint(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectPrint(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralPrint(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokePrint(spNo);
            }
        }else if(name.equals(Global.CARD)){ // 补卡
            if(statuChangeEvent == 1){ // 创建补卡申请
                RepairCardApplyForm repairCardApplyForm = new RepairCardApplyForm();
                repairCardApplyForm.setRepairCardNumber(approveForm.getSpNo()); // 补卡编号
                repairCardApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 补卡人员ID
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("PunchCorrection")){ // 补卡组件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("补卡")){
                                repairCardApplyForm.setAbnormal(contentsItem.getValue().getPunchCorrection().getState());
                                Long repairCardTime = Long.parseLong(contentsItem.getValue().getPunchCorrection().getTime().toString());
                                repairCardApplyForm.setRepairCardTime(LocalDateTimeUtils.convertDateToLDT(new Date(repairCardTime*1000))); // 补卡时间
                            }
                        }
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("补卡事由")){
                                repairCardApplyForm.setReason(contentsItem.getValue().getText());
                            }
                        }
                    }else if(contentsItem.getControl().equals("File")){ // 附件控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("说明附件")){
                                repairCardApplyForm.setAnnex(null);
                            }
                        }
                    }
                }
                String approverIds = null;
                int index = 0;
                for (ApproveForm.SpRecord spRecord : approveForm.getSpRecords()) {
                    for (int i = 0; i < spRecord.getDetails().size(); i++) {
                        if(index == 0){
                            approverIds = spRecord.getDetails().get(i).getApprover().getUserId();
                        }else{
                            approverIds += "," + spRecord.getDetails().get(i).getApprover().getUserId();
                        }
                        index++;
                    }
                }
                String ccIds = null;
                for (int i = 0; i < approveForm.getNotifyers().size(); i++) {
                    if(i == 0){
                        ccIds = approveForm.getNotifyers().get(i).getUserId();
                    }else{
                        ccIds += "," + approveForm.getNotifyers().get(i).getUserId();
                    }
                }
                repairCardApplyForm.setApproverIds(approverIds); // 审批人IDS
                repairCardApplyForm.setCcIds(ccIds); // 抄送人IDS
                repairCardApplyForm.setStatus(RepairCardApply.Status.stayProcess); // 状态
                repairCardApplyForm.setSource(RepairCardApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addRepairCardApply(repairCardApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeRepairCard(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectRepairCard(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralRepairCard(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeRepairCard(spNo);
            }
        }else if(name.equals(Global.CHECK_IN)){ // 入住
            if(statuChangeEvent == 1){ // 创建入住申请
                LiveApplyForm liveApplyForm = new LiveApplyForm();
                liveApplyForm.setCode(approveForm.getSpNo()); // 入住编号
                liveApplyForm.setUserId(approveForm.getApplyer().getUserId()); // 入住人员ID
                // 通过人员ID查询人员数据
                User user = userService.getById(approveForm.getApplyer().getUserId());
                liveApplyForm.setCompanyId(user.getCompanyId()); // 入住人员所属企业
                liveApplyForm.setDepartmentId(user.getDepartmentId()); // 入住人员所属部门
                liveApplyForm.setPostId(user.getPostId().toString()); // 入住人员岗位
                if(user.getSex().equals(User.Sex.female)){
                    liveApplyForm.setSex(LiveApply.Sex.female);
                }else{
                    liveApplyForm.setSex(LiveApply.Sex.male);
                }
                liveApplyForm.setAge(user.getAge());
                if(null != user.getMaritalStatus()){
                    if(user.getMaritalStatus().equals(User.MaritalStatus.unmarried)){
                        liveApplyForm.setMaritalStatus(LiveApply.MaritalStatus.unmarried);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.married)){
                        liveApplyForm.setMaritalStatus(LiveApply.MaritalStatus.married);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.divorced)){
                        liveApplyForm.setMaritalStatus(LiveApply.MaritalStatus.divorced);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.widowed)){
                        liveApplyForm.setMaritalStatus(LiveApply.MaritalStatus.widowed);
                    }
                }else{
                    liveApplyForm.setMaritalStatus(LiveApply.MaritalStatus.unmarried);
                }
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Date")){ // 日期/日期+时间控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("开始入住时间")){
                                Long usePrintDate = Long.parseLong(contentsItem.getValue().getDate().getTimestamp());
                                liveApplyForm.setLivingTime(LocalDateTimeUtils.convertDateToLDT(new Date(usePrintDate*1000)));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("备注信息")){
                                liveApplyForm.setRemark(contentsItem.getValue().getText());
                            }
                        }
                    }
                }
                liveApplyForm.setStatus(LiveApply.Status.pending); // 状态
                liveApplyForm.setSource(LiveApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addLiveApply(liveApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeLive(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectLive(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralLive(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeLive(spNo);
            }
        }else if(name.equals(Global.CHANGE_ROOM)){ // 换房
            if(statuChangeEvent == 1){ // 创建换房申请
                DeployApplyForm deployApplyForm = new DeployApplyForm();
                deployApplyForm.setCode(approveForm.getSpNo()); // 换房编号
                deployApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 换房人员ID
                // 通过人员ID查询人员数据
                User user = userService.getById(approveForm.getApplyer().getUserId());
                deployApplyForm.setCompanyId(user.getCompanyId()); // 换房人员所属企业
                deployApplyForm.setDepartmentId(user.getDepartmentId()); // 换房人员所属部门
                deployApplyForm.setPostId(user.getPostId().toString()); // 换房人员岗位
                if(user.getSex().equals(User.Sex.female)){
                    deployApplyForm.setSex(DeployApply.Sex.female);
                }else{
                    deployApplyForm.setSex(DeployApply.Sex.male);
                }
                deployApplyForm.setAge(user.getAge());
                if(null != user.getMaritalStatus()){
                    if(user.getMaritalStatus().equals(User.MaritalStatus.unmarried)){
                        deployApplyForm.setMaritalStatus(DeployApply.MaritalStatus.unmarried);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.married)){
                        deployApplyForm.setMaritalStatus(DeployApply.MaritalStatus.married);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.divorced)){
                        deployApplyForm.setMaritalStatus(DeployApply.MaritalStatus.divorced);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.widowed)){
                        deployApplyForm.setMaritalStatus(DeployApply.MaritalStatus.widowed);
                    }
                }else{
                    deployApplyForm.setMaritalStatus(DeployApply.MaritalStatus.unmarried);
                }
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Text")){ // 文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("原房型")){
                                // 通过名称查询房屋类型
                                RoomCategory roomCategory = roomCategoryService.getOne(new QueryWrapper<RoomCategory>().eq("name", contentsItem.getValue().getText()));
                                if(roomCategory != null){
                                    deployApplyForm.setRoomCategoryId(roomCategory.getId());
                                }
                            }else if(titleItem.getText().equals("原房号")){
                                // 通过名称查询房号
                                RoomSn roomSn = roomSnService.getOne(new QueryWrapper<RoomSn>().eq("code", contentsItem.getValue().getText()));
                                if(roomSn != null){
                                    deployApplyForm.setRoomSnId(roomSn.getId());
                                    if(null != roomSn.getFloorId()){
                                        deployApplyForm.setFloorId(Long.parseLong(roomSn.getFloorId()));
                                        Floor floor = floorService.getById(roomSn.getFloorId());
                                        if(floor != null){
                                            deployApplyForm.setBuildingId(Long.parseLong(floor.getBuildingId()));
                                        }
                                    }
                                    deployApplyForm.setRoomStatusId(Long.parseLong(roomSn.getRoomStatusId()));
                                }
                            }else if(titleItem.getText().equals("原床位号")){
                                deployApplyForm.setBedSnId(Long.parseLong(contentsItem.getValue().getText()));
                            }else if(titleItem.getText().equals("换房房型")){
                                // 通过名称查询房屋类型
                                RoomCategory roomCategory = roomCategoryService.getOne(new QueryWrapper<RoomCategory>().eq("name", contentsItem.getValue().getText()));
                                if(roomCategory != null){
                                    deployApplyForm.setApplyRoomCategoryId(roomCategory.getId());
                                }
                            }else if(titleItem.getText().equals("换房床位")){
                                deployApplyForm.setApplyBedSnId(Long.parseLong(contentsItem.getValue().getText()));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Date")){ // 日期/日期+时间控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("计划入住时间")){
                                Long usePrintDate = Long.parseLong(contentsItem.getValue().getDate().getTimestamp());
                                deployApplyForm.setLivingTime(LocalDateTimeUtils.convertDateToLDT(new Date(usePrintDate*1000)));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("备注信息")){
                                deployApplyForm.setRemark(contentsItem.getValue().getText());
                            }
                        }
                    }
                }
                deployApplyForm.setStatus(DeployApply.Status.pending); // 状态
                deployApplyForm.setSource(DeployApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addDeployApply(deployApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeDeploy(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectDeploy(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralDeploy(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeDeploy(spNo);
            }
        }else if(name.equals(Global.CHECK_OUT)){ // 退房
            if(statuChangeEvent == 1){ // 创建退房申请
                CheckOutApplyForm checkOutApplyForm = new CheckOutApplyForm();
                checkOutApplyForm.setCode(approveForm.getSpNo()); // 退房编号
                checkOutApplyForm.setUserId(Long.parseLong(approveForm.getApplyer().getUserId())); // 退房人员ID
                // 通过人员ID查询人员数据
                User user = userService.getById(approveForm.getApplyer().getUserId());
                checkOutApplyForm.setCompanyId(user.getCompanyId()); // 退房人员所属企业
                checkOutApplyForm.setDepartmentId(user.getDepartmentId()); // 退房人员所属部门
                checkOutApplyForm.setPostId(user.getPostId().toString()); // 退房人员岗位
                if(user.getSex().equals(User.Sex.female)){
                    checkOutApplyForm.setSex(CheckOutApply.Sex.female);
                }else{
                    checkOutApplyForm.setSex(CheckOutApply.Sex.male);
                }
                checkOutApplyForm.setAge(user.getAge());
                if(null != user.getMaritalStatus()){
                    if(user.getMaritalStatus().equals(User.MaritalStatus.unmarried)){
                        checkOutApplyForm.setMaritalStatus(CheckOutApply.MaritalStatus.unmarried);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.married)){
                        checkOutApplyForm.setMaritalStatus(CheckOutApply.MaritalStatus.married);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.divorced)){
                        checkOutApplyForm.setMaritalStatus(CheckOutApply.MaritalStatus.divorced);
                    }else if(user.getMaritalStatus().equals(User.MaritalStatus.widowed)){
                        checkOutApplyForm.setMaritalStatus(CheckOutApply.MaritalStatus.widowed);
                    }
                }else{
                    checkOutApplyForm.setMaritalStatus(CheckOutApply.MaritalStatus.unmarried);
                }
                List<ApprovalDetailResp.Info.ApplyData.ContentsItem> contentsItems = approvalDetailResp.getInfo().getApplyData().getContents();
                for (ApprovalDetailResp.Info.ApplyData.ContentsItem contentsItem: contentsItems) {
                    if(contentsItem.getControl().equals("Text")){ // 文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("退房房号")){
                                // 通过名称查询房号
                                RoomSn roomSn = roomSnService.getOne(new QueryWrapper<RoomSn>().eq("code", contentsItem.getValue().getText()));
                                if(roomSn != null){
                                    checkOutApplyForm.setRoomSnId(roomSn.getId());
                                    checkOutApplyForm.setFloorId(Long.parseLong(roomSn.getFloorId()));
                                    Floor floor = floorService.getById(roomSn.getFloorId());
                                    if(floor != null){
                                        checkOutApplyForm.setBuildingId(Long.parseLong(floor.getBuildingId()));
                                    }
                                    RoomCategory roomCategory = roomCategoryService.getById(roomSn.getRoomCategoryId());
                                    if(roomCategory != null){
                                        checkOutApplyForm.setRoomCategoryId(roomCategory.getId());
                                    }
                                }
                            }else if(titleItem.getText().equals("退房床位")){
                                checkOutApplyForm.setBedSnId(Long.parseLong(contentsItem.getValue().getText()));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Date")){ // 日期/日期+时间控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("搬离时间")){
                                Long usePrintDate = Long.parseLong(contentsItem.getValue().getDate().getTimestamp());
                                checkOutApplyForm.setCheckOutTime(LocalDateTimeUtils.convertDateToLDT(new Date(usePrintDate*1000)));
                            }
                        }
                    }else if(contentsItem.getControl().equals("Textarea")){ // 多行文本控件
                        for (TitleItem titleItem : contentsItem.getTitle()) {
                            if(titleItem.getText().equals("备注信息")){
                                checkOutApplyForm.setRemark(contentsItem.getValue().getText());
                            }
                        }
                    }
                }
                checkOutApplyForm.setStatus(CheckOutApply.Status.pending); // 状态
                checkOutApplyForm.setSource(CheckOutApply.Source.enterpriseWeChat); // 数据来源
                receiveService.addCheckOutApply(checkOutApplyForm);
            }else if(statuChangeEvent == 2){ // 同意
                receiveService.agreeCheckOut(spNo);
            }else if(statuChangeEvent == 3){ // 驳回
                receiveService.rejectCheckOut(spNo);
            }else if(statuChangeEvent == 4){ // 转审
                // receiveService.referralCheckOut(spNo);
            }else if(statuChangeEvent == 6){ // 撤销
                receiveService.revokeCheckOut(spNo);
            }
        }
    }

    */
/**
     * 数据流输出
     * @param outputStream
     * @param text
     * @return
     *//*

    private boolean outputStreamWrite(OutputStream outputStream, String text){
        try {
            outputStream.write(text.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
*/
